package com.numberone.master.modules.sys.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.sys.entity.Opinion;
import com.numberone.master.modules.sys.entity.User;
import com.numberone.master.modules.sys.service.OpinionService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * 意见反馈Controller
 * @author ck
 * @version 2018-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/opinion")
public class OpinionController extends BaseController {

	@Autowired
	private OpinionService opinionService;

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public Opinion get(@RequestParam(required=false) String id) {
		Opinion entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = opinionService.get(id);
		}
		if (entity == null){
			entity = new Opinion();
		}
		return entity;
	}

	 /**
     * list列表
     * @param opinion
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("sys:opinion:list")
	@RequestMapping(value = {"list", ""})
	public String list(Opinion opinion, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if(!user.getId().equals("1274")){
			opinion.setCreateById(user.getId());
		}
		Page<Opinion> page = opinionService.findPage(new Page<Opinion>(request, response), opinion); 
		model.addAttribute("page", page);
		return "modules/sys/opinionList";
	}

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("sys:opinion:save")
	@RequestMapping(value = "openSave")
	public String openSave() {
		return "modules/sys/opinionSave";
	}


	/**
	 * 打开修改页面
	 * @param opinion
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:opinion:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(Opinion opinion, Model model) {
		model.addAttribute("opinion", opinion);
		return "modules/sys/opinionUpdate";
	}

	/**
	 * 添加
	 * @param opinion
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("sys:opinion:save")
	@RequestMapping(value = "save")
	public String save(Opinion opinion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, opinion)){
			return openSave();
		}
		User user = UserUtils.getUser();
		opinion.setCreateBy(user);
		opinion.setState("0");
		opinion.setCreateDate(new Date());
		opinionService.save(opinion);
		addMessage(redirectAttributes, "添加意见成功");
		return "redirect:"+Global.getAdminPath()+"/sys/opinion/?repage";
	}

	/**
	 * 修改
	 * @param opinion
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("sys:opinion:update")
	@RequestMapping(value = "update")
	public String update(Opinion opinion, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, opinion)){
			return openUpdate(opinion, model);
		}
		User user = UserUtils.getUser();
		opinion.setUpdateBy(user);
		opinion.setState("1");
		opinion.setUpdateDate(new Date());
		opinionService.update(opinion);
		addMessage(redirectAttributes, "修改意见成功");
		return "redirect:"+Global.getAdminPath()+"/sys/opinion/?repage";
	}

	 /**
     * 删除
     * @param opinion
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("sys:opinion:delete")
	@RequestMapping(value = "delete")
	public String delete(Opinion opinion, RedirectAttributes redirectAttributes) {
		opinionService.delete(opinion);
		addMessage(redirectAttributes, "删除意见成功");
		return "redirect:"+Global.getAdminPath()+"/sys/opinion/?repage";
	}

}